package village.rainbow.slipin.util;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

/**
 * 
 * @author KJ
 *
 * Screen Utility Class
 */
public class ScreenUtil
{
	private float mScreenDensity = 0.0f;
	private int mScreenDPI = 0;
	private int mScreenWidth = 0;
	private int mScreenHeight = 0;
	private int mScreenRatioX = 0;
	private int mScreenRatioY = 0;
	private int mScreenFullRatioX = 0;
	private int mScreenFullRatioY = 0;
	private int mIndicatorHeight = 0;
	private float mScaledDensity = 0.0f;
	private float mChildRatioW = 1.0f;
	private float mChildRatioH = 1.0f;
	
	private static ScreenUtil INSTANCE = null;
	
	public static ScreenUtil get()
	{
		if (INSTANCE == null)
			INSTANCE = new ScreenUtil();
		
		return INSTANCE;
	}
	private ScreenUtil()
	{
		
	}
	
	/**
	 * Tablet or Hanset 구분
	 * 
	 * @param context
	 * @return true : Tablet, false : Handset
	 */
	public boolean isTablet(Context context)
	{
		boolean xlarge = ((context.getResources().getConfiguration().screenLayout & 
				Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
	    boolean large = ((context.getResources().getConfiguration().screenLayout & 
	    		Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
	    
	    return (xlarge || large);
	}
	/**
	 * 화면 정보얻기
	 * @param activity
	 */
	public void setDisplayInfo(Activity activity)
	{
		// 화면 스크린정보를 얻는다.		
		WindowManager wm = activity.getWindowManager();
		Display display = wm.getDefaultDisplay();	
		DisplayMetrics dm = new DisplayMetrics();
		display.getMetrics(dm);
		
		mScreenDensity = dm.density;
		mScreenDPI = dm.densityDpi;
		mScreenWidth = display.getWidth();
		mScreenHeight = display.getHeight();
		mScaledDensity = dm.scaledDensity;
		
		// indicator 높이 구하기
		Rect outRect = new Rect();
		activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
		mIndicatorHeight = outRect.top;			
		// 높이와 넓이의 최대공약수를 구해서 화면비율을 구한다.
		// indicator 제외
		MathUtil mathUtil = new MathUtil();
		int ratioH = mScreenHeight - mIndicatorHeight;
		int gcd = mathUtil.getGCD(mScreenWidth, ratioH);
		mScreenRatioX = (int) (mScreenWidth / gcd);
		mScreenRatioY = (int) (ratioH / gcd);
		// Full
		gcd = mathUtil.getGCD(mScreenWidth, mScreenHeight);
		mScreenFullRatioX = (int) (mScreenWidth / gcd);
		mScreenFullRatioY = (int) (mScreenHeight / gcd);
	}
	/**
	 * 인디케이터를 뺸 화면높이
	 * @return
	 */
	public int getNotIndicatorHeight()
	{
		return mScreenHeight - mIndicatorHeight;
	}
	/**
	 * 화면이 가로모드일때 인디케이터를 뺸 화면높이
	 * @return
	 */
	public int getLandNotIndicatorHeight()
	{
		return mScreenWidth - mIndicatorHeight;
	}
	
	/**
	 * 화면 비율을 구한다.
	 * @param w : 화면넓이
	 * @param h : 화면높이
	 * @return (ScreenRatioItem) 화면정보 Item Class 객체
	 */
	public ScreenRatioItem getScreenRatio(int w, int h)
	{
		ScreenRatioItem result = new ScreenRatioItem();
		
		// 최대공약수를 구한다.
		int gcd = MathUtil.getGCD(w, h);
		
		// 최대공약수를 이용해 화면비율을 구한다.
		result.setRatiioW((int)((w * 1.0) / (gcd) * 1.0));
		result.setRatiioH((int)((h * 1.0) / (gcd) * 1.0));
		
		return result;
	}
	
	////////////////////////////////////////
	// getter, setter

	public float getScreenDensity() {return mScreenDensity; }
	public int getScreenDPI() { return mScreenDPI; }
	public int getScreenWidth() { return mScreenWidth; }
	public int getScreenHeight() { return mScreenHeight; }
	public int getScreenRatioX() { return mScreenRatioX; }
	public int getScreenRatioY() { return mScreenRatioY; }
	public int getScreenFullRatioX() { return mScreenFullRatioX; }
	public int getScreenFullRatioY() { return mScreenFullRatioY; }
	public int getIndicatorHeight() { return mIndicatorHeight; }
	public float getScaledDensity() { return mScaledDensity; }
	public void setChildRatioW(float ratio) { mChildRatioW = ratio; }
	public float getChildRatioW() { return mChildRatioW; }
	public void setChildRatioH(float ratio) { mChildRatioH = ratio; }
	public float getChildRatioH() { return mChildRatioH; }
	
	////////////////////////////////////////	
	// 화면비율 정보 Item Class
	
	public class ScreenRatioItem
	{
		private float mRatioW = 0;
		private float mRatioH = 0;
		
		public void setRatiioW(float ratioW) { mRatioW = ratioW; }
		public void setRatiioH(float ratioH) { mRatioH = ratioH; }
		public float getRatiioW() { return mRatioW; }
		public float getRatiioH() { return mRatioH; }
	}
	
	/**
	 * 파일명으로 리소스 ID를 얻어온다.
	 * 
	 * @param context 
	 * @param resType : Resource Type
	 * @param fileName : 파일명
	 * @return
	 */
	public static int getResIDByFileName(Context context, String resType, String fileName)
	{
		String packageName = context.getPackageName();
		
		return context.getResources().getIdentifier(packageName + ":" + resType + "/" + fileName, null, null); 
	}
	
	/**
	 * 대상점과 중심점이 이루는 각도를 구한다.
	 * 
	 * @param centerPoint : 중심점
	 * @param destPoint : 대상점
	 * @return
	 */
	public static double getAngle(PointF centerPoint, PointF destPoint)
	{
		return Math.atan2(destPoint.y-centerPoint.y, destPoint.x-centerPoint.x) * 180 / (Math.atan(1) * 4);
	}
	
	/**
	 * View의 Background로 설정된 이미지 해제
	 * 
	 * @param view
	 */
	public static void unusedViewImage(View view)
	{
		Drawable drawable = view.getBackground();
		
		if (drawable != null)
			drawable.setCallback(null);
		
		view.setBackgroundDrawable(null);
	}
	// 현재 view가 Activity상에 위치하는 X 좌표얻기
	public static int getLeftInActivity(View view)
	{
	    if (view.getParent() == view.getRootView())
	    	return view.getLeft();
	    else
	        return view.getLeft() + getLeftInActivity((View)view.getParent());
	}
	// 현재 view가 Activity상에 위치하는 Y 좌표얻기
	public static int getTopInActivity(View view) 
	{
	    if (view.getParent() == view.getRootView())
	    	return view.getTop();
	    else
	        return view.getTop() + getTopInActivity((View)view.getParent());
	}
	
	////////////////////////////////////////	
}
